﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using yncTech.Common;
using yncTech.BusinessObjects;

public partial class Admin_Fin_Fin002a : AdminBasePage
{
    decimal boxID = -1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[VBoxReceiptsMetadata.ColumnNames.BoxID] != null)
        {
            boxID = decimal.Parse(Request.QueryString[VBoxReceiptsMetadata.ColumnNames.BoxID]);
        }
        if (!IsPostBack)
        {
            drpYear.Items.Add(new ListItem(DateTime.Now.AddYears(-1).Year.ToString(), DateTime.Now.AddYears(-1).Year.ToString()));
            drpYear.Items.Add(new ListItem(DateTime.Now.Year.ToString(), DateTime.Now.Year.ToString()));
            drpYear.SelectedValue = DateTime.Now.Year.ToString();

            for (int i = 1; i <= 12; i++)
            {
                drpMonth.Items.Add(new ListItem(i.ToString(), i.ToString()));
            }

            drpMonth.SelectedValue = DateTime.Now.Month.ToString();

            switch (ActionType)
            {
                case OperationType.Add:
                    //GetBoxNo();
                    btnSave.Text = "新增";
                    break;
                case OperationType.Update:
                    btnSave.Text = "修改";
                    drpYear.Enabled = false;
                    drpMonth.Enabled = false;
                    VBoxReceipts vbox = new VBoxReceipts();
                    vbox.Query.Where(vbox.Query.BoxID == boxID);
                    vbox.Query.Load();
                    drpYear.SelectedValue = vbox.BoxYear.ToString();
                    drpMonth.SelectedValue = vbox.BoxMonth.ToString();
                    drpDocType.SelectedValue = vbox.DocumentType;
                    drpLocation.SelectedValue = vbox.BoxLocation.ToString();
                    drpFunctionDept.SelectedValue = vbox.FunctionDept;
                    drpCompany.SelectedValue = vbox.CompanyCode;
                    if (vbox.ReceiptCount.HasValue && vbox.ReceiptCount.Value > 0)
                    {
                        drpDocType.Enabled = false;
                        drpFunctionDept.Enabled = false;
                        drpLocation.Enabled = false;
                    }
                    drpCompany.Enabled = false;

                    //if (vbox.IsOpen.HasValue && vbox.IsOpen.Value)
                    //{
                    //    radYes.Checked = true;
                    //}
                    //else
                    //{
                    //    radNo.Checked = true;
                    //}

                    break;
                case OperationType.Delete:
                    btnSave_Click(null, null);
                    break;

            }

        }
    }

   

    protected void OnYorMChanged(object sender, EventArgs e)
    {
        //GetBoxNo();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            lbMessage.Text = "";
            FinBox box = new FinBox();

            switch (ActionType)
            {
                case OperationType.Add:
                    int index = 1;
                    FinBox lastBox = new FinBox();
                    lastBox.Query.Where(lastBox.Query.CompanyCode == drpCompany.SelectedValue).OrderBy(lastBox.Query.BoxID.Descending);
                    lastBox.Query.es.Top = 1;
                    lastBox.Query.Load();
                    if (lastBox.es.HasData)
                    {
                        if (lastBox.CreatedDttm.Value.Year == DateTime.Now.Year)
                        {
                            index = lastBox.BoxIndex.Value + 1;
                        }
                    }

                    box.CompanyCode = drpCompany.SelectedValue;
                    box.BoxIndex = index;
                    box.BoxYear = int.Parse(drpYear.SelectedValue);
                    box.BoxMonth = int.Parse(drpMonth.SelectedValue);
                    box.StorageStatus = "S2";
                    box.BoxLocation = decimal.Parse(drpLocation.SelectedValue);
                    box.FunctionDept = drpFunctionDept.SelectedValue;
                    box.DocumentType = drpDocType.SelectedValue;
                    //box.IsOpen = radYes.Checked;
                    box.CreatedBy = box.UpdatedBy = LoginUserInfo.CurrentUser.LoginName;
                    box.UpdatedDttm = box.CreatedDttm = DateTime.Now;
                    box.Save();
                    break;
                case OperationType.Update:
                    box.LoadByPrimaryKey(boxID);
                    if (drpLocation.Enabled)
                    {
                        box.BoxLocation = decimal.Parse(drpLocation.SelectedValue);
                        box.FunctionDept = drpFunctionDept.SelectedValue;
                        box.DocumentType = drpDocType.SelectedValue;
                    }
                    //box.IsOpen = radYes.Checked;
                    box.UpdatedBy = LoginUserInfo.CurrentUser.LoginName;
                    box.UpdatedDttm = DateTime.Now;
                    box.Save();
                    break;
                case OperationType.Delete:
                    box.LoadByPrimaryKey(boxID);
                    //box.IsOpen = !box.IsOpen;
                    box.Save();
                    WebHelper.GotoParent();
                    break;

            }
            WebHelper.CloseDialog(true);
        }
        catch (Exception ex)
        {
            lbMessage.Text = ex.Message;
        }
    }
}